package dpatterns.observer.trademonster.pull;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import dpatterns.observer.trademonster.push.StockData;
import dpatterns.observer.trademonster.push.Stock;

public class StockPublisher implements Observer, Display {
	private Map<String, Stock> stocks;
	private Observable observable;
	
	public StockPublisher(Observable observable) {
		stocks = new HashMap<String, Stock>();
		this.observable = observable;
		observable.addObserver(this);
	}
	
	@Override
	public void update(Observable obs, Object arg) {
		if(obs instanceof StockData) {
			StockData stockData = (StockData) obs;
			stocks = stockData.getStocks();
		}
	}

	@Override
	public void display() {
		for(Map.Entry<String, Stock> stock : stocks.entrySet()) {
			System.out.println(stock.getValue().getCompany()+" ("+stock.getValue().getTicker()+"), Price "+stock.getValue().getPrice()+"$, Change: "+stock.getValue().getChange()+"%");
		}
	}

}
